﻿using UnityEngine;

[CreateAssetMenu(menuName = "New Skill", fileName = "Skill")]
public class SkillData : ScriptableObject
{
    // ID
    public int skillID;
    // 图片
    public Sprite skillSprite;
    // 名称
    public string skillName;
    // 当前等级
    public int skillLevel;
    // 最大等级
    public int maxSkillLevel;

    // 描述
    [TextArea(1, 6)]
    public string skillDes;
    // 是否已解锁
    public bool isUnlocked;
    // 解锁所需前置技能
    public SkillData[] preSkills;

    public bool CanUpgrade()
    {
        return isUnlocked && skillLevel < maxSkillLevel;
    }

    // 是否可以解锁，只有当前技能是锁定状态时才有效
    public bool CanUnlock()
    {
        if (isUnlocked)
        {
            return false;
        }
        if(preSkills == null || preSkills.Length == 0)
        {
            return true;
        }
        bool canUnlockFlag = true;
        foreach (SkillData sdata in preSkills)
        {
            canUnlockFlag = canUnlockFlag && sdata.isUnlocked && sdata.skillLevel > 0;
            if (!canUnlockFlag)
            {
                return false;
            }
        }
        return canUnlockFlag;
    }


    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
        {
            return false;
        }

        return skillID == ((SkillData)obj).skillID;
    }

    public override int GetHashCode()
    {
        return $"{skillID}_{skillName}".GetHashCode();
    }
}
